from prompt_toolkit import PromptSession
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.history import FileHistory
import click
import importlib
import inspect
from loguru import logger


CMDCompleter = WordCompleter(['restart', 'start', 'close', 'clear'], ignore_case=True)

def display_usage():pass

def display_pyenv_info():
    logger.info("# 3. pyenv info display --> ")
    logger.info("# https://pyenv-win.github.io/pyenv-win/")
    logger.info("pyenv version")
    logger.info('python -c "import sys; print(sys.executable)"')
    logger.info("# 4. show all installed python")
    logger.info("pyenv versions")
    logger.info("# 4. show all installed python")
    logger.info("pyenv global 3.5.2")
    logger.info("# 4. show all installed python")
    logger.info("pyenv local 3.8.2")

def display_poetry_info():
    logger.info("# 激活pyenv之后, ")
    logger.info("# https://zhuanlan.zhihu.com/p/445952026")
    logger.info("# 5. poetry info display --> ")
    logger.info("# poetry self update # 更新")
    logger.info("# poetry --version ")
    logger.info("# poetry new poetry_demo #  创建项目")
    logger.info("# 可以修改 pyproject.toml 然后")
    logger.info("# 初始化包 poetry init")
    logger.info("# tool.poetry.dependencies 指定依赖")
    logger.info("# poetry add pendulum 添加新的依赖")
    logger.info("# poetry run python your_script.py 运行项目")
    logger.info("# 通过 virtualenvs.create=true 控制虚拟环境")
    logger.info("# poetry env use python3")
    logger.info("# 激活虚拟环境 poetry shell")
    logger.info("#  显示虚拟环境绝对路径 poetry env list --full-path")


def display_way():
    logger.info("create new env.")
    logger.info("# pyenv install py38 -> poetry new demo or init -> poetry env use py38 ")
    logger.info("don't create new env.")
    logger.info("# pyenv install py38 -> poetry new demo or init and virtualenvs.create=false -> poetry env use py38 ")
    
# @click.command()
# @click.option('--help', prompt='Enter your name')
def cmd_app_main():
    # ethercat_master_thread = threading.Thread(target=ethercat.ethercat_master_loop)
    # ethercat_master_thread.start()
    
    session = PromptSession(
        history=FileHistory('history.txt'),
        auto_suggest=AutoSuggestFromHistory(),
        completer=CMDCompleter,)

    while True:
        try:
            text = session.prompt('> ')
            click.echo_via_pager(text)
        except KeyboardInterrupt:
            continue
        except EOFError:
            break
        else:
            print('You entered:', text)
            if text == 'help':
                display_usage()
                display_pyenv_info()
            elif text == 'clear':
                click.clear()
            elif text == 'pyenv':
                click.clear()
    
    
    # ethercat_master_thread.ethercat_master_stop()
    # print('GoodBye!')

if __name__ == '__main__':
    cmd_app_main()
